﻿using HPIT_LiuzuPoint.Business;
using HPIT_LiuzuPoint.Entity.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HPIT_LiuzuPoint.Web.Controllers
{
    public class receptionController : Controller
    {
        private UserlistService _UserlistService = new UserlistService();
        private UsersService _UsersService = new UsersService();
        public ActionResult Index()
        {
            //获取会员等级下拉框所需的数据源
            ViewBag.Ulist = GetUlist();

            return View();
        }

        private List<SelectListItem> GetUlist()
        {
            //查询会员等级的信息
            //下拉框
            var ulist = _UsersService.GetList(c => true).Select(c => new SelectListItem()
            {
                Text = c.RoomName,
                Value = c.Id.ToString()
            }).ToList();

            ulist.Insert(0, new SelectListItem() { Text = "全部", Value = "0" });

            return ulist;

            //using (var db = new UserEntity())
            //{
            //    var ulist = db.Room.Where(c => true).Select(c => new SelectListItem() { Text = c.RoomName, Value = c.Id.ToString() }).ToList();
            //    ulist.Insert(0, new SelectListItem() { Text = "全部", Value = "0" });
            //    return ulist;
            //}
        }
        [HttpPost]
        public ActionResult UserList(SearchViewModel viewModel)
        {
            //调用业务逻辑层中的分页方法
            var result = _UserlistService.UserList(viewModel);

            //OperateResult中的Data属性包含的分页的数据
            return Json(result.Data);
        }

        [HttpGet]
        public ActionResult Add()
        {
            ViewBag.Ulist = GetUlist();
            return View("Edit");
        }
        [HttpPost]
        public ActionResult Add(ReceptionEditViewModel viewModel)
        {
            var result = _UserlistService.AddUserlist(viewModel);
            return Json(result);
        }

         [HttpGet]
        public ActionResult Edit(int id)
        {
            ViewBag.Ulist = GetUlist();

            var result = _UserlistService.GetUserlist(id);
            if (result.State == ResultSatae.Success)
            {
                return View(result.Data);
            }
            else
            {
                return HttpNotFound();
            }
        }
        [HttpPost]
        public ActionResult Edit(ReceptionEditViewModel viewModel)
        {
            var result = _UserlistService.Edituserlist(viewModel);
            return Json(result);
        }

        [HttpPost]
        public ActionResult Delete(int deleteId)
        {
            var result = _UserlistService.Deleteuserlist(deleteId);
            return Json(result);
        }
    }
}